package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SyArea;
import cn.com.bdqn.entity.SyDept;
import cn.com.bdqn.service.SyDeptService;
import cn.com.bdqn.utils.ReturnData;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/syDept")
@Api(tags = "部门管理接口")
public class SyDeptController {

    @Autowired
    private SyDeptService syDeptService;

    @Operation(summary = "查询所有部门信息")
    @PostMapping("/queryAll")
    public ReturnData queryAll(){
        try {
            List<SyDept> syDepts = syDeptService.queryAll();
            return ReturnData.ok().put("syDepts",syDepts);
        }catch (Exception e){
            return ReturnData.currError("查询所有部门信息数据库异常");
        }
    }

    @Operation(summary = "根据ID查询单个部门信息")
    @PostMapping("/queryById")
    public ReturnData queryById(Long id){
        try {
            SyDept syDept = syDeptService.queryById(id);
            return ReturnData.ok().put("syDept",syDept);
        }catch (Exception e){
            return ReturnData.currError("查询单个部门信息数据库异常");
        }
    }

    @Operation(summary = "根据Id删除部门信息")
    @PostMapping("/delSyDept")
    public ReturnData delSyDept(Long id){
        int result = syDeptService.delSyDept(id);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("删除部门信息数据库异常");
        }
    }

    @Operation(summary = "新增部门信息")
    @PostMapping("/addSyDept")
    public ReturnData addSyDept(SyDept syDept){
        int result = syDeptService.addSyDept(syDept);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("新增部门信息数据库异常");
        }
    }

    @Operation(summary = "修改部门信息")
    @PostMapping("/updateSyDept")
    public ReturnData updateSyDept(SyDept syDept){
        int result = syDeptService.updateSyDept(syDept);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("修改部门信息数据库异常");
        }
    }
}
